package com.lq.JDBC.Service.impl;

import com.lq.JDBC.Dao.UserDao;
import com.lq.JDBC.Dao.impl.UserDaoImpl;
import com.lq.JDBC.Service.UserService;
import com.lq.JDBC.Utils.Transaction;

public class UserServiceImpl implements UserService {

    //new一个Dao层的实现
    private UserDao userDao = new UserDaoImpl();

    @Override
    public Boolean transferAccount(int formID, int toId, int money) {
        //抽象出一个Transaction对象来处理事务
        Transaction.begin();
        try{
            userDao.transfer(formID,-money);
            userDao.transfer(toId,money);

            Transaction.commit();
        }catch (Exception e) {
            e.printStackTrace();

            Transaction.rollback();
        } finally {

            Transaction.close();
        }

        Transaction.begin();
        try{
            userDao.transfer(formID,-money);
            userDao.transfer(toId,money);

            Transaction.commit();
        }catch (Exception e) {
            e.printStackTrace();

            Transaction.rollback();
        } finally {

            Transaction.close();
        }
        return false;
    }

    //这里仅作为测试,把数据库中编号一的用户转账给编号二的用户500
    public static void main(String[] args) {

        UserService userService = new UserServiceImpl();
        userService.transferAccount(1,2,500);
    }
}
